package store.dao;

import store.model.Account;
import store.model.Goods;
import store.model.Recover;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 关于goods表的所有操作
 */
public class GoodsDao {

    private static DBUtils<Goods> dbUtils;

    private static Recover<Goods> goodsRecover = new Goods.GoodsRecover();

    public GoodsDao() {
        if (dbUtils == null) {
            dbUtils = new DBUtils<>();
        }
    }

    public List<Goods> selectAll() throws SQLException, ClassNotFoundException {
        String sql = "select * from goods";
        return dbUtils.executeQuery1(sql, goodsRecover);
    }

    public Goods selectOne(int goods_no) throws SQLException, ClassNotFoundException {
        String sql = "select * from goods where goods_no = ?";
        List<Goods> goodsList = dbUtils.executeQuery1(sql, goodsRecover, goods_no);
        return goodsList.size() > 0 ? goodsList.get(0) : null;
    }

    public int updateCount(Goods goods) throws SQLException, ClassNotFoundException {
        String sql = "update goods set count = ? where goods_no = ?";
        Object[] values = new Object[]{
                goods.getCount(),
                goods.getGoodNo()
        };
        return dbUtils.executeUpdate(sql, values);
    }


    public int insert(Goods goods) throws SQLException, ClassNotFoundException {
        String sql = "insert into goods(goods_no,goods_name,cost,price, count,category_no) values(?,?,?,?,?,?)";
        Object[] values = new Object[]{
                goods.getGoodNo(),
                goods.getGoodsName(),
                goods.getCost(),
                goods.getPrice(),
                goods.getCount(),
                goods.getCategory_no()
        };
        return dbUtils.executeUpdate(sql, values);
    }
}
